<?php

namespace App\Func\System;

use App\Factory\Sms\AliyunSmsSender;
use App\Factory\Sms\SmsSenderInterface;

class SmsSenderFunc
{
    private $engine = null;

    public function setSmsEngine(SmsSenderInterface $sender){
        $this->engine = $sender;
        return $this;
    }

    /**
     * @param $phoneNumber 手机号
     * @param $templateCode 模板code
     * @param $templateParam 模板对应参数
     * @param array $args 自定义参数
     * @param false $debug 是否debug
     * @return bool|\Illuminate\Http\JsonResponse
     */
    public function sendSms($phoneNumber,$templateCode,$templateParam,$args = [],$debug = false){
        if($this->engine instanceof AliyunSmsSender){
            $templateParam = json_encode($templateParam);
                if(!isset($args['signName'])){
                    return fail_return(null,'阿里云引擎短信请传参$args["signName"]');
                }
            }
        if(!$this->engine){
            return fail_return(null,'短信发送失败,请检查引擎');
        }

        $res = $this->engine->sendSMS($phoneNumber,$templateCode,$templateParam,$args,$debug);
        if(!$res){
            return fail_return(null,'短信发送失败');
        }
        return true;
    }
}
